﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.AcmsControls.ContentControls.Admin;
using ClimateChangeChampionsData;

namespace ClimateChangeChampionsCustomAdmin
{
    public partial class OldData : BaseCustomAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gridSchools.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(gridSchools_ItemCommand);
        }

        void gridSchools_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Convert")
            {
                if (Page.IsValid)
                {
                    int id = (int) gridSchools.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
                    DataContext context = new DataContext();
                    School school = School.GetById(id, context);
                    if (school != null)
                    {
                        Acms.Bll.UserSecurity user = Acms.Bll.UserSecurity.CreateInstance(AcmsSite.Id);
                        user.Address1 = school.Address1;
                        user.Address2 = school.Address2;
                        user.County = school.County;
                        user.EmailAddress = school.ContactEmail;
                        user.FirstName = school.ContactFirstName;
                        user.LastName = school.ContactLastName;
                        user.MobileTel = school.ContactTel;
                        user.PostCode = school.PostCode;
                        user.Town = school.Town;
                        user.UserName = school.ContactEmail;
                        user.SetCustomProperty("School Name", school.SchoolName.Trim());
                        user.SetCustomProperty("School Type", school.Type.ToString());
                        user.SetCustomProperty("School Tel", school.SchoolTel.Trim());
                        user.SetCustomProperty("Secondary Contact Name", school.SecondaryContactName.Trim());
                        user.SetPassword(null, school.Password);
                        user.Update(school.Created);
                    }
                }
            }
        }


        protected bool CheckCreated(object emailAddress)
        {
            if (emailAddress != null)
            {
                Acms.Bll.User user = Acms.Bll.User.GetByEmailAddress(emailAddress.ToString(), AcmsSite.Id);
                return user != null;
            }
            else
            {
                return false;
            }
        }
    }
}